/************************************************************************
 * The code is owned by mainframer.cn and Shang Tao
 * Without aproval of mainframer.cn and Shang Tao, removing the copyright
 * infomation is regarded as invassion of proprietary copyright
 ***********************************************************************/
package com.mr.qa.action;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.mr.qa.GlobalConfigs;
import com.mr.qa.exception.QaException;

/*
 * setting the config paramter values used by administrator
 */


public class SetGlobalConfigsAction extends BaseAction
{
    private static final Log logger = LogFactory.getLog(SetGlobalConfigsAction.class);
    public SetGlobalConfigsAction()
    {
    }
    public ActionForward doAction(ActionMapping mapping, ActionForm form,
                        HttpServletRequest request, HttpServletResponse response) throws QaException
    {
        String action = this.getAction(request);
        ActionMessages errors = new ActionMessages();
        if("setglobalconfigs".equals(action))
        {
            /*
            GlobalConfigs.smtp_server = request.getParameter("smtp_server");
            logger.debug("smtp_server="+GlobalConfigs.smtp_server);
            GlobalConfigs.smtp_server_password = request.getParameter("smtp_server_password");
            GlobalConfigs.smtp_server_username = request.getParameter("smtp_server_username");
            GlobalConfigs.from_email_address = request.getParameter("from_email_address");
            String to_email_address = request.getParameter("to_email_address");
            try
            {
                EMail mail=new EMail();
                mail.setAddress(to_email_address, EMail.TO);
                mail.setFromAddress(GlobalConfigs.from_email_address);
                mail.setSMTPHost(GlobalConfigs.smtp_server,
                GlobalConfigs.smtp_server_username,
                GlobalConfigs.smtp_server_password);
                mail.setSubject("mail test");
                mail.setHtmlBody("administrator of www.mainframer.cn is testing the mail sending");
                mail.sendBatch();
            }catch(Exception e)
            {
                String exceptionMsg = e.getMessage();
                //System.out.println("test mail exception:"+exceptionMsg);
                errors.add("testmail",new ActionMessage("message",exceptionMsg));
            }
            */
            action = "globalconfigs";
        }
        else if("setsizes".equals(action))
        {
            try
            {
                GlobalConfigs.SIZE_LATESTNEWS_HOMEPAGE = new Integer(request.getParameter("size_latestnews_homepage")).intValue();
                GlobalConfigs.SIZE_LATESTLIVES_HOMEPAGE = new Integer(request.getParameter("size_latestlives_homepage")).intValue();
                GlobalConfigs.SIZE_PART_1_HOMEPAGE = new Integer(request.getParameter("size_part_1_homepage")).intValue();
                GlobalConfigs.SIZE_PART_2_HOMEPAGE = new Integer(request.getParameter("size_part_2_homepage")).intValue();
                GlobalConfigs.SIZE_PART_3_HOMEPAGE = new Integer(request.getParameter("size_part_3_homepage")).intValue();
                GlobalConfigs.SIZE_PART_4_HOMEPAGE = new Integer(request.getParameter("size_part_4_homepage")).intValue();

                GlobalConfigs.Q_VIEW_COUNTS = new Integer(request.getParameter("q_view_counts")).intValue();
                GlobalConfigs.Q_ANSWER_COUNTS = new Integer(request.getParameter("q_answer_counts")).intValue();
                GlobalConfigs.NEWS_LIST_URL = request.getParameter("news_list_url");
                GlobalConfigs.NEWS_LIST_SIZE = new Integer(request.getParameter("news_list_size"));
                GlobalConfigs.SIZE_QUESTION_LIST = new Integer(request.getParameter("size_question_list")).intValue();
                GlobalConfigs.SIZE_CATEGORY_QUESTION_LIST = new Integer(request.getParameter("size_category_question_list")).intValue();
                GlobalConfigs.SIZE_LIFE_LIST = new Integer(request.getParameter("size_life_list")).intValue();
                GlobalConfigs.SIZE_CATEGORY_LIFE_LIST = new Integer(request.getParameter("size_category_life_list")).intValue();
                GlobalConfigs.SIZE_TECH_LIST = new Integer(request.getParameter("size_tech_list")).intValue();
                GlobalConfigs.SIZE_CATEGORY_TECH_LIST = new Integer(request.getParameter("size_category_tech_list")).intValue();
                
                GlobalConfigs.NUMBER_ANSWER_FOR_VIEW_QUESTION = new Integer(request.getParameter("number_answer_for_view_question")).intValue();
                GlobalConfigs.SIZE_SHAREDFILE_LIST = new Integer(request.getParameter("sharedfile_list_size")).intValue();
                GlobalConfigs.SIZE_SHAREDFILES_HOMEPAGE = new Integer(request.getParameter("size_sharedfiles_homepage")).intValue();
                //2009.4.25
                GlobalConfigs.QUESTION_EXPIRY_NUMBER = new Integer(request.getParameter("question_expiry_number")).intValue();
                //GlobalConfigs.QUESTION_EXPIRY_UNIT = request.getParameter("question_expiry_unit");
                GlobalConfigs.ANSWER_EXPIRY_NUMBER = new Integer(request.getParameter("answer_expiry_number")).intValue();
                GlobalConfigs.QUESTION_LOCK_NUMBER = new Integer(request.getParameter("question_lock_number")).intValue();
                GlobalConfigs.QUESTION_CAN_DELETE_DURATION_AFTER_ADD = new Integer(request.getParameter("QUESTION_CAN_DELETE_DURATION_AFTER_ADD")).intValue();
                GlobalConfigs.ANSWER_CAN_DELETE_DURATION_AFTER_ADD = new Integer(request.getParameter("ANSWER_CAN_DELETE_DURATION_AFTER_ADD")).intValue();
                
                GlobalConfigs.SIZE_GONGGAO_HOMEPAGE = new Integer(request.getParameter("size_gonggao_homepage")).intValue();
                GlobalConfigs.SIZE_HELP_HOMEPAGE = new Integer(request.getParameter("size_help_homepage")).intValue();
                GlobalConfigs.QUESTION_LOCK_NUMBER = new Integer(request.getParameter("question_lock_number")).intValue();
                
                GlobalConfigs.MOST_ACTIVE_USER_COUNT_QUESTION = new Integer(request.getParameter("most_active_user_count_question")).intValue();
                GlobalConfigs.MOST_ACTIVE_USER_COUNT_TECH = new Integer(request.getParameter("most_active_user_count_tech")).intValue();

            }
            catch(Exception e)
            {
                String exceptionMsg = e.getMessage();
                errors.add("testmail",new ActionMessage("message",exceptionMsg));
            }
            action = "globalconfigs";
        }
        else if("setsecurity".equals(action))
        {
            GlobalConfigs.PROHIBITED_WORDS = request.getParameter("prohibited_words");
            GlobalConfigs.HOURS_ADDQUESTION_AFTER_REGISTRATION = new Integer(request.getParameter("hours_addquestion_after_registration")).intValue();
            GlobalConfigs.HOURS_ADD_SHARED_FILE_AFTER_REGISTRATION = new Integer(request.getParameter("hours_add_shared_file_after_registration"));
            GlobalConfigs.PROHIBITED_USERS = request.getParameter("prohibited_users");
            GlobalConfigs.NUMBER_QUESTIONS_A_USER_ONE_DAY = new Integer(request.getParameter("number_questions_a_user_one_day")).intValue();
            GlobalConfigs.NUMBER_IMAGE_A_USER_ONE_DAY = new Integer(request.getParameter("number_images_a_user_one_day")).intValue();
            GlobalConfigs.NUMBER_SHARED_FILE_A_USER_ONE_DAY = new Integer(request.getParameter("number_shared_file_a_user_one_day")).intValue();
            GlobalConfigs.NUMBER_SHARED_FILE_A_USER_DOWNLOAD_LIMIT_SCORE = new Integer(request.getParameter("number_shared_file_a_user_download_limit_score")).intValue();
            GlobalConfigs.NUMBER_SHARED_FILE_A_USER_DOWNLOAD_ONE_DAY = new Integer(request.getParameter("number_shared_file_a_user_download_one_day")).intValue();
        }
        else if("sysupdate".equals(action))
        {
            //GlobalConfigs.isApplicationUnderUpdate = true;
            String broadcastNews = null;
			try {
				broadcastNews = new String(request.getParameter("broadcast_news").getBytes("iso-8859-1"), "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
            if(broadcastNews == null || broadcastNews.trim().length() == 0)
            {
                GlobalConfigs.BROADCAST_NEWS = "";
            }
            else
            {
                GlobalConfigs.BROADCAST_NEWS = broadcastNews;
            }
            action = "globalconfigs";
        }
        else if("setuserlistsize".equals(action))
        {
            try
            {
                GlobalConfigs.SIZE_USER_LIST = new Integer(request.getParameter("size_user_list"));
            }catch(Exception e){}
        }
        else
        {
            action = "globalconfigs";
        }
        if(errors.size() > 0)           
            saveErrors(request, errors);
        return mapping.findForward(action);
    }
}
